package cn.kgc.easybuy.service.impl;

import cn.kgc.easybuy.dao.UserMapper;
import cn.kgc.easybuy.pojo.User;
import cn.kgc.easybuy.service.UserService;
import cn.kgc.easybuy.utils.SendMailUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-10 10:57
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> selectUserByPage(Map<String, Object> params) {
        PageHelper.startPage(params);
        List<User> users = userMapper.selectUserByPage(params);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User login(String loginName, String password) {
        User user = userMapper.login(loginName);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误！");
        }
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("用户名或密码错误！");
        }
        return user;
    }

    @Override
    public boolean delUser(Integer id) {
        return userMapper.delUser(id) > 0?true:false;
    }

    @Override
    public boolean addUser(User user) {

        return userMapper.addUser(user) > 0?true:false;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user) > 0?true:false;
    }


    //所有验证判断 重复返回false 不重复返回true
    @Override
    public boolean checkEmail(String email) {
        return userMapper.checkEmail(email) > 0 ? false:true;
    }
    @Override
    public boolean checkLoginName(String loginName) {
        return  userMapper.checkLoginName(loginName) > 0?false:true;
    }

    @Override
    public boolean checkMobile(String mobile) {
        return userMapper.checkMobile(mobile) > 0 ? false:true;
    }

    @Override
    public boolean checkIdentityCode(String identityCode) {
        return userMapper.checkIdentityCode(identityCode) > 0 ? false:true;
    }

    @Override
    public boolean checkPassword(User user) {

        return userMapper.checkPassword(user) > 0;
    }

    @Override
    public User selectUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public boolean sendEmail(String email, String code) {
        SendMailUtil sendMailUtil = new SendMailUtil();
        boolean isSend = sendMailUtil.sendTextEmail(email,"易买网注册验证码",code);
        return isSend;
    }


}
